package top.ahzy.seobench.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ahzy.seobench.mapper.ConfigDataMapper;
import top.ahzy.seobench.model.entity.ConfigData;
import top.ahzy.seobench.result.Result;
import top.ahzy.seobench.service.ConfigDataService;
import top.ahzy.seobench.util.DefaultDataUtil;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class ConfigDataServiceImpl implements ConfigDataService {
    @Resource
    ConfigDataMapper configDataMapper;

    @Autowired
    DefaultDataUtil defaultDataUtil;
    @Override
    public Result<String> getDetail(String userId) {
        LambdaQueryWrapper<ConfigData> configDataWrapper = new LambdaQueryWrapper<>();
        configDataWrapper.eq(ConfigData::getUserId,userId)
                .orderByDesc(ConfigData::getCreateTime)
                .last("LIMIT 1");
        ConfigData configData = configDataMapper.selectOne(configDataWrapper);
        if(ObjUtil.isNull(configData)){
            return Result.error("数据不存在!!");
        }
        return Result.success(JSONUtil.parseObj(configData.getDetail()));
    }

    @Override
    public Result<String> edit(String userId,String detail) {
        ConfigData configData = new ConfigData();
        configData.setId(IdUtil.simpleUUID())
                .setUserId(userId)
                .setDetail(detail)
                .setCreateTime(new Date());
        configDataMapper.insert(configData);
        return Result.success();
    }
}
